Skip to content

Add default_headers request option to skip the client's default headers - #13

Merged
zyc9012 merged 3 commits into
mainfrom
default-headers
Nov 27, 2026
Merged

zyc9012 merged 3 commits into
mainfrom
default-headers

Conversation

@trusche

@trusche trusche commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Opus 5.5 High

The client's emulation headers go out on every request, and a request can't remove them. Setting a header to nil skips it, and a per-request emulation: false doesn't remove client defaults either. So a Chrome emulation sends navigation headers (upgrade-insecure-requests: 1, sec-fetch-user: ?1, priority: u=0, i) also on requests where Chrome never sends them, such as a script load or a fetch().

Bumps the version to 0.7.0. Publishing still needs the v0.7.0 tag after merge.

Fix

wreq 0.16.1 already has this: RequestBuilder::default_headers(false) (vendor/wreq/src/client/request.rs:287) skips the client's default headers for one request. This PR passes the new per-request option default_headers: through to it in apply_request_options. Batch requests use the same function, so they support it too.

client = Wreq::Client.new(emulation: "chrome_143", user_agent: ua)
client.get(url, default_headers: false, headers: {"user-agent" => ua, "accept" => "*/*", "sec-fetch-dest" => "script", ...})

What it does, checked on the wire with tls.peet.ws:

  • It skips the emulation's headers, the client's user_agent and its headers. The caller sends every header it wants.
  • A per-request emulation: still adds its own headers, because wreq writes them into the request itself (request.rs:774).
  • The TLS and HTTP/2 fingerprint don't change. A fresh client gives JA4 t13d1516h2 and Akamai hash 52d84b11 with and without the option.
  • Headers go out in the order of the Ruby hash. Reversing the hash reverses the wire order, so callers pass headers in the browser's order. The README says so.

Before and after on the wire

The same request through a fresh chrome_143 client, with default_headers: false and the 12 headers Chrome sends when it loads a script. tls.peet.ws reports what it received:

0.6.1 (option ignored) 0.7.0
JA4 t13d1516h2_8daaf6152771_d8a2da3f94cd same
HTTP/2 Akamai hash 52d84b11737d980aef856699f885ca86 same
Headers sent 14 12, exactly the ones passed
upgrade-insecure-requests: 1 sent not sent
sec-fetch-user: ?1 sent not sent

The headers go out in the order of the Ruby hash. The hash below is not in Chrome's order; it only shows which headers are sent.

Headers received
== 0.6.1
sec-ch-ua: "Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
accept: */*
sec-fetch-site: same-site
sec-fetch-mode: no-cors
sec-fetch-user: ?1
sec-fetch-dest: script
accept-encoding: gzip, deflate, br, zstd
accept-language: en-US,en;q=0.9
priority: u=1
referer: https://example.com/

== 0.7.0
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
accept: */*
accept-encoding: gzip, deflate, br, zstd
accept-language: en-US,en;q=0.9
sec-ch-ua: "Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-site: same-site
sec-fetch-mode: no-cors
sec-fetch-dest: script
referer: https://serpapi.com/
priority: u=1
Script
require "json"
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36"
script_headers = {
  "user-agent" => ua, "accept" => "*/*", "accept-encoding" => "gzip, deflate, br, zstd", "accept-language" => "en-US,en;q=0.9",
  "sec-ch-ua" => %q{"Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"}, "sec-ch-ua-mobile" => "?0", "sec-ch-ua-platform" => '"Windows"',
  "sec-fetch-site" => "same-site", "sec-fetch-mode" => "no-cors", "sec-fetch-dest" => "script", "referer" => "https://example.com/", "priority" => "u=1",
}
client = Wreq::Client.new(emulation: "chrome_143", emulation_os: "windows", user_agent: ua)
json = JSON.parse(client.get("https://tls.peet.ws/api/all", headers: script_headers, default_headers: false).body)
sent = json.dig("http2", "sent_frames").find { |frame| frame["frame_type"] == "HEADERS" }["headers"].reject { |line| line.start_with?(":") }
puts "wreq-rb #{Wreq::VERSION}: JA4 #{json.dig("tls", "ja4")}, Akamai hash #{json.dig("http2", "akamai_fingerprint_hash")}"
puts sent

Testing

  • New live test test_default_headers_false_sends_only_request_headers (httpbun, like the other request tests). It checks that the defaults are sent without the option, and that Sec-Fetch-User, Upgrade-Insecure-Requests, Sec-Ch-Ua, User-Agent and the client's headers are not sent with it. Without the change it fails with Expected "?1" to be nil.
  • bundle exec rake test: 59 runs, 184 assertions, 0 failures. Rust tests: 1 passed.
  • Built locally with rustc 1.100. wreq 0.16.1 needs 1.98 or newer.

trusche and others added 2 commits November 26, 2026 09:34
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@trusche
trusche requested review from zyc9012 and a balanced review from Copilot and removed request for zyc9012 November 26, 2026 09:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The README’s header-order claim needs qualification because header_order remains active with default_headers: false and can override Ruby hash order.

1 open issue
What changed in this PR

Adds a per-request default_headers option to the Ruby client, allowing callers to skip client-level headers while preserving TLS and HTTP/2 emulation settings.

Changes:

  • Passes default_headers through the shared request-options handler, supporting individual and batch requests.
  • Documents the option and adds a live regression test for suppressing client defaults.
  • Updates Ruby and Rust package versions to 0.7.0.
File Description
test/​request_test.rb Tests that client defaults are omitted while request headers remain.
README.md Documents the option and provides a usage example.
lib/​wreq-rb/​version.rb Bumps the Ruby version to 0.7.0.
ext/​wreq_rb/​src/​client.rs Forwards the option to the underlying request builder.
ext/​wreq_rb/​Cargo.toml Bumps the Rust extension version to 0.7.0.
Cargo.lock Synchronizes the locked extension version.

Review effort: Balanced

Comment thread README.md Resolved
Clarified comments regarding TLS, HTTP/2 settings, and header order.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@trusche trusche self-assigned this Nov 26, 2026

@zyc9012 zyc9012 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @trusche 👍

@zyc9012
zyc9012 merged commit 076794e into main Nov 27, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants